package com;

import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class MyHandlerMapping implements HandlerMapping {

    /**
     * /a ---->com.AHandler
     * /b----->com.BHandler
     */
    private Map<String, Class<?>> urlMapping = null;

    public void setUrlMapping(Map<String, Class<?>> urlMapping) {
        this.urlMapping = urlMapping;
    }

    @Override
    public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
        String uri = request.getRequestURI();
        if (urlMapping.containsKey(uri)) {
            Class<?> aClass = urlMapping.get(uri);
            // o就是一个Handler
            Object o = aClass.newInstance();
            return new HandlerExecutionChain(o);
        }
        return null;
    }
}
